<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	关注回复和默认回复调整：调整为选择回复规则
	如何选择呢？
	下拉框选择肯定不行，最好是弹出框选择
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'default_back';
$return=array();
$return['set_types']=array(
	'1'=>'直接设置回复内容',
	'2'=>'从已有消息回复中选取',
);
if($_RQ['op']=='default_back'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array_fields(array('type','content','back_id'),$post);
		$setting=array('value'=>ijson_encode($data));
		$check=pdo_getcolumn('core_account_setting',array('uniacid'=>$_SESSION['uniacid'],'key'=>'default_back'),'id');
		if($check){
			pdo_update('core_account_setting',$setting,array('id'=>$check));
		}else{
			$setting['uniacid']=$_SESSION['uniacid'];
			$setting['key']='default_back';
			pdo_insert('core_account_setting',$setting);
		}
		exi('设置成功！','',array('op'=>'default_back'));
	}else{
		$item=uni_setting('default_back');
		$return['item']=$item?$item:array();
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置粉丝发送的消息没有相应回复规则处理时触发的回复规则',
			'edit'=>array(
				array('type'=>'radio','name'=>'type','title'=>'选择设置方式','options'=>'set_types'),
				array('type'=>'string','name'=>'content','title'=>'设置回复内容','show'=>array('type',1),'help'=>'设置回复内容，仅支持文本。复杂的回复内容请先添加回复规则，然后在此处选择。可用{nickname}作为粉丝昵称，系统将在回复内容时，把回复内容中的{nickname}替换为粉丝昵称！'),
				array('type'=>'choose','name'=>'back_id','title'=>'选择回复规则','show'=>array('type',2),'action'=>'wechat.specal_back','op'=>'choose','plugin'=>'core','choose_title'=>'选择回复内容'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='follow_back'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array_fields(array('type','content','back_id','credit1','scancontent'),$post);
		$setting=array('value'=>ijson_encode($data));
		$check=pdo_getcolumn('core_account_setting',array('uniacid'=>$_SESSION['uniacid'],'key'=>'follow_back'),'id');
		if($check){
			pdo_update('core_account_setting',$setting,array('id'=>$check));
		}else{
			$setting['uniacid']=$_SESSION['uniacid'];
			$setting['key']='follow_back';
			pdo_insert('core_account_setting',$setting);
		}
		exi('设置成功！','',array('op'=>'follow_back'));
	}else{
		$item=uni_setting('follow_back');
		//读取传参二维码
		if(!$item['qrurl']){
			$title = '关注二维码';
			$keyid='follow_back';
			$getqrdata=array(
				'keyid'=>$keyid,
				'scantype'=>'follow',
			);
			$getqrdata = ijson_encode($getqrdata);
			$qr = cfc('account')->get_qr(2,'core',$getqrdata,30,$title,$keyid);
			$item['qrurl']=$qr['url'];
		}
		$return['item']=$item?$item:array();
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置粉丝关注时触发的回复规则',
			'edit'=>array(
				array('type'=>'radio','name'=>'type','title'=>'选择设置方式','options'=>'set_types'),
				array('type'=>'string','name'=>'content','title'=>'设置回复内容','show'=>array('type',1),'help'=>'设置回复内容，仅支持文本。复杂的回复内容请先添加回复规则，然后在此处选择。可用{nickname}作为粉丝昵称，系统将在回复内容时，把回复内容中的{nickname}替换为粉丝昵称！'),
				array('type'=>'choose','name'=>'back_id','title'=>'选择回复规则','show'=>array('type',2),'action'=>'wechat.specal_back','op'=>'choose','plugin'=>'core','choose_title'=>'选择回复内容'),
				array('type'=>'ewm','name'=>'qrurl','title'=>'扫码关注二维码','help'=>'【<red>温馨提示</red>】：只有已经添加了认证服务号，这里才会显示关注二维码！'),
				array('type'=>'string','name'=>'credit1','title'=>'扫码关注奖励积分数'),
				array('type'=>'text_wx','name'=>'scancontent','title'=>'扫码关注回复','help'=>'关键词匹配：{nickname}表示昵称，{credit1}表示奖励积分数'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='choose'){
	$params=array('status'=>1);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','backtype');
	$list=pdo_getslice('core_back',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	$list[]=array('id'=>0,'title'=>'不设置默认回复');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['backtypes']=get_back_types();
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'未开启的回复规则，不会显示在本列！','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'规则名称','type'=>'string'),
			array('name'=>'backtype','title'=>'回复类型','type'=>'radio','options'=>'backtypes'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>